package com.example.shishaolong.warehousemanager;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shishaolong.warehousemanager.common.DateUtil;
import com.example.shishaolong.warehousemanager.common.SystemConst;
import com.example.shishaolong.warehousemanager.dao.AdminDao;
import com.example.shishaolong.warehousemanager.dao.GoodsDao;
import com.example.shishaolong.warehousemanager.entity.AdminEntity;
import com.example.shishaolong.warehousemanager.entity.GoodsEntity;

public class UserMainActivity extends AppCompatActivity {

    //管理员id
    private int adminId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_main);

        AdminDao adminDao = new AdminDao(this);

        //界面组件绑定
        TextView adminName = (TextView) findViewById(R.id.user_main_admin_name_value);
        TextView adminSex = (TextView) findViewById(R.id.user_main_admin_sex_value);
        TextView adminLevel = (TextView) findViewById(R.id.user_main_admin_level_value);
        TextView addDate = (TextView) findViewById(R.id.user_main_admin_add_date_value);
        Button modifyInfoBtn = (Button) findViewById(R.id.user_main_modify_info_btn);
        Button goodsManageBtn = (Button) findViewById(R.id.user_main_goods_manage_btn);
        Button adminManageBtn = (Button) findViewById(R.id.user_main_admin_manage_btn);


        //判断用户角色，隐藏按钮
        adminId = getIntent().getExtras().getInt(SystemConst.ADMIN_ENTITY_ID, -1);
        if (adminId > 0) {
            AdminEntity adminEntity = adminDao.findById(adminId);
            if (adminEntity == null) {
                Toast.makeText(this, "获取用户信息失败，请重新登录！", Toast.LENGTH_SHORT);
                finish();//关闭当前activity
            } else {
                if (adminEntity.getLevel() == 1) {
                    adminManageBtn.setVisibility(View.VISIBLE);
                }
                //界面组件注入值
                adminName.setText(adminEntity.getName());
                adminSex.setText(adminEntity.getSex() == 1 ? "男" : "女");
                adminLevel.setText(adminEntity.getLevel() == 1 ? "超级管理员" : "普通管理员");
                addDate.setText(adminEntity.getAddDate());
            }
        }

    }

    //启动货物列表页面
    public void goods_manage(View view) {
        Intent goodsActivityIntent = new Intent();
        goodsActivityIntent.setClass(this, GoodsActivity.class);
        startActivity(goodsActivityIntent);
    }

    //启动管理员列表页面
    public void admin_manage(View view) {
        Intent AdminActivityIntent = new Intent();
        AdminActivityIntent.setClass(this, AdminActivity.class);
        startActivity(AdminActivityIntent);
    }

    //修改个人信息
    public void modify_info(View view) {
        Intent updateAdminIntent = new Intent();
        updateAdminIntent.putExtra(SystemConst.ADMIN_GOTO_FROM,SystemConst.ADMIN_GOTO_FROM_USER_INFO);
        updateAdminIntent.putExtra(SystemConst.ADMIN_ENTITY_ID, adminId); //将id传入
        updateAdminIntent.setClass(this,AdminAddActivity.class);
        //跳转到修改页面，并设置了页面标识符（用于所跳转页面返回到当前页面，能判断出是哪个页面）
        startActivity(updateAdminIntent);   //启动修改页面
    }
}
